import { FormDataRowType } from '/types/form.d';
import _ from 'lodash-es'

/**
 *Map数据转对象
 *
 * @export
 * @param {Map<string, string>} strMap
 * @return {FormDataRowType}  FormDataTypeRow
 */
 
export function strMapToObj(strMap:Map<string, string>):FormDataRowType {
  let obj = Object.create(null);
  for (let [k,v] of strMap) {
    obj[k] = v;
  }
  return obj;
}


/**
 * @description 延迟计时器
 * @export
 * @param {(number | string)} time
 * @returns {*}  
 */
export function sleep(time: number | string): Promise<void> {
  return new Promise<void>((resolve, reject) => {
    setTimeout(() => {
      resolve()
    }, +time);
  })
}


/**
 * @description Object提取键值对
 * @export
 * @param {object} [data=undefined]
 * @param {string} [key='key']
 * @param {string} [value='value']
 * @returns {*}  
 */
export function objTKV (data:ObjectLikeType[] = [], key = 'key', value = 'value') {
  const rData:ObjectLikeType = {}
  data.forEach(e => {
    if (e &&_.isObjectLike(e)) {
      rData[e[key]] = e[value] || undefined
    }
  })

  return rData
}